package my.diplom.dao.impl;

import my.diplom.dao.IOrderDAO;
import my.diplom.entity.Order;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 14.07.2009
 * Time: 16:10:03
 * To change this template use File | Settings | File Templates.
 */
public class OrderDAOImpl implements IOrderDAO {

    private static final Logger log = Logger.getLogger(OrderDAOImpl.class);

    private HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    public void create(Order order) {
        log.debug("create order");
        template.save(order);
    }

    public void update(Order order) {
        log.debug("update order");
        template.saveOrUpdate(order);
    }

    public Order getById(Long orderId) {
        log.debug("get order by id = " + orderId);
        return (Order) template.get(Order.class, orderId);
    }

    public List<Order> getAll() {
        log.debug("get all orders");
        //noinspection JpaQlInspection
        return template.find("from Order order by startDate");
    }

    public void delete(Order order) {
        log.debug("delete order");
        template.delete(order);
    }


    @Override
    public List<Order> getByLogin(String login) {
        log.debug("get by login " + login);
        return template.find("from Order o where o.user.login = ?", login);
    }
}
